package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.UpdateStockBean;
import com.shop2cn.iapi.agentproducts.parameter.UpdateStockDto;
import com.shop2cn.iapi.agentproducts.service.UpdateStockCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.stockmgr.agentinventoryExample;
import com.ymttest.database.model.stockmgr.agentinventorysku;
import com.ymttest.database.model.stockmgr.agentinventoryskuExample;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryWapper;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryskuWapper;
import com.ymttest.utils.AgentUsers;

/**
*修改商品库存
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateStock{
	private static UpdateStockBean updatestockBean;
	private static UpdateStockCall updatestockCall;
	
	agentinventoryExample inventoryExample=new agentinventoryExample();
	agentinventoryWapper inventory=new agentinventoryWapper();
	
	agentinventoryskuExample skuExample=new agentinventoryskuExample();
	agentinventoryskuWapper sku=new agentinventoryskuWapper();
	

	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改商品库存");
	}

	@Before
	public void caseUp() {
		updatestockBean=new UpdateStockBean();
		updatestockCall=new UpdateStockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatestockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyMongoCurMq(String pid) throws ClientProtocolException, IOException, InterruptedException {
        Thread.sleep(5000);
        MessageListBean MessageListBean2 = MqQueryCallService.initAgentMongoCrud(pid);
        List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
        Logger.debug(joa2);
        int redo=0;
		while(joa2.size()<2&&redo<3) {
			Thread.sleep(5000);
			joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			redo++;
		}
        String actionType = "ProductStockChange";
        String appId = "agentproducts.iapi.ymatou.com";
        Logger.verifyEquals(actionType,joa2.get(0).get("ActionType"),"验证ActionType");
        Logger.verifyEquals(pid,joa2.get(0).get("ProductId"),"验证ProductId");
        Logger.verifyEquals(appId,joa2.get(0).get("AppId"),"验证AppId");
    }
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_01() {
		Logger.start(true,"渠道商修改商品单规格库存-修改库存比之前少");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			updatestockBean.setUserId(vipRootUserId);
			updatestockBean.setProductId(productId);
			UpdateStockDto updateStockDto = new UpdateStockDto();
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
			String catalogId = ymtCatalogsIList.get(0).getsCatalogId();
			updateStockDto.setCatalogId(catalogId);
			int num = 10;
			updateStockDto.setStockNum(num);

			List<UpdateStockDto> updateStockDtos = new ArrayList();
			updateStockDtos.add(updateStockDto);
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("200",updatestockCall.getString("code"),"验证code");
			YmtCatalogsI ymtCatalogsI = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogId);
			Logger.verifyEquals(num,ymtCatalogsI.getiNum(),"验证库存");

			verifyMongoCurMq(productId);
			 //验证 会调用prodstockmgr生成出入库单
			Logger.comment("################入库后的数量################");
    		skuExample.createCriteria().andProductIdEqualTo(productId).andCatalogIdEqualTo(catalogId);
    		List<agentinventorysku> skus=sku.selectByExample(skuExample);

    		Logger.verifyEquals(100, skus.get(0).getNum(), "验证新增入库单第一条明细");
    		Logger.verifyEquals(-90, skus.get(1).getNum(), "验证新增入库单第二 条明细");
    		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_02() {
		Logger.start(true,"渠道商修改商品多规格库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			updatestockBean.setUserId(vipRootUserId);
			updatestockBean.setProductId(productId);
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);

			int num = 20;
			List<UpdateStockDto> updateStockDtos = new ArrayList();
			ymtCatalogsIList.forEach(p->{
				UpdateStockDto updateStockDto = new UpdateStockDto();
				updateStockDto.setCatalogId(p.getsCatalogId());
				updateStockDto.setStockNum(num);
				updateStockDtos.add(updateStockDto);
			});
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("200",updatestockCall.getString("code"),"验证code");
			ymtCatalogsIList.forEach(x->{
				try {
					YmtCatalogsI ymtCatalogsI = new YmtCatalogsIWapper().selectCatalogsByCatalogId(x.getsCatalogId());
					Logger.verifyEquals(num,ymtCatalogsI.getiNum(),"验证库存");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_03() {
		Logger.start(true,"渠道商修改商品库存，规格不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			updatestockBean.setUserId(vipRootUserId);
			updatestockBean.setProductId(productId);
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);

			int num = 20;
			List<UpdateStockDto> updateStockDtos = new ArrayList();
			ymtCatalogsIList.forEach(p->{
				UpdateStockDto updateStockDto = new UpdateStockDto();
				updateStockDto.setCatalogId(p.getsCatalogId()+1);
				updateStockDto.setStockNum(num);
				updateStockDtos.add(updateStockDto);
			});
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("201",updatestockCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_04() {
		Logger.start(true,"商品Id不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			updatestockBean.setUserId(vipRootUserId);
			UpdateStockDto updateStockDto = new UpdateStockDto();
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
			String catalogId = ymtCatalogsIList.get(0).getsCatalogId();
			updateStockDto.setCatalogId(catalogId);
			int num = 10;
			updateStockDto.setStockNum(num);

			List<UpdateStockDto> updateStockDtos = new ArrayList();
			updateStockDtos.add(updateStockDto);
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("400",updatestockCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_05() {
		Logger.start(true,"增加虚拟库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			updatestockBean.setUserId(vipRootUserId);
			updatestockBean.setProductId(productId);
			UpdateStockDto updateStockDto = new UpdateStockDto();
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
			String catalogId = ymtCatalogsIList.get(0).getsCatalogId();
			updateStockDto.setCatalogId(catalogId);
			int num = 10;
			int virtualNum=20;
			updateStockDto.setStockNum(num);
			updateStockDto.setVirtualStockNum(virtualNum);

			List<UpdateStockDto> updateStockDtos = new ArrayList();
			updateStockDtos.add(updateStockDto);
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("200",updatestockCall.getString("code"),"验证code");
			YmtCatalogsI ymtCatalogsI = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogId);
			Logger.verifyEquals(num+virtualNum,ymtCatalogsI.getiNum(),"验证库存");
			Logger.verifyEquals(virtualNum,ymtCatalogsI.getVirtualStock(),"验证虚拟库存");
			Logger.verifyEquals(num,ymtCatalogsI.getiNum()-ymtCatalogsI.getVirtualStock(),"验证真实库存");
			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_06() {
		Logger.start(true,"虚拟库存为负数，验证失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			updatestockBean.setUserId(vipRootUserId);
			updatestockBean.setProductId(productId);
			UpdateStockDto updateStockDto = new UpdateStockDto();
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
			String catalogId = ymtCatalogsIList.get(0).getsCatalogId();
			updateStockDto.setCatalogId(catalogId);
			int num = 10;
			updateStockDto.setStockNum(num);
			updateStockDto.setVirtualStockNum(-3);

			List<UpdateStockDto> updateStockDtos = new ArrayList();
			updateStockDtos.add(updateStockDto);
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("400",updatestockCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateStock_07() {
		Logger.start(true,"渠道商修改商品单规格库存-修改库存比之前库存多");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean)    ;
            Thread.sleep(2000);
			
			updatestockBean.setUserId(vipRootUserId);
			updatestockBean.setProductId(productId);
			UpdateStockDto updateStockDto = new UpdateStockDto();
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
			String catalogId = ymtCatalogsIList.get(0).getsCatalogId();
			updateStockDto.setCatalogId(catalogId);
			int num = 300;
			updateStockDto.setStockNum(num);

			List<UpdateStockDto> updateStockDtos = new ArrayList();
			updateStockDtos.add(updateStockDto);
			updatestockBean.setUpdateStockList(updateStockDtos);
			updatestockCall.setData(updatestockBean);
			updatestockCall.callService();
			Logger.verifyEquals("200",updatestockCall.getString("code"),"验证code");
			YmtCatalogsI ymtCatalogsI = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogId);
			Logger.verifyEquals(num,ymtCatalogsI.getiNum(),"验证库存");
			
			verifyMongoCurMq(productId);
             //验证出入库明细
			 //验证 会调用prodstockmgr生成出入库单
			Logger.comment("################入库后的数量################");
    		skuExample.createCriteria().andProductIdEqualTo(productId).andCatalogIdEqualTo(catalogId);
    		List<agentinventorysku> skus=sku.selectByExample(skuExample);

    		Logger.verifyEquals(100, skus.get(0).getNum(), "验证新增入库单第一条明细");
    		Logger.verifyEquals(200, skus.get(1).getNum(), "验证新增入库单第二 条明细");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}